﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Common;

namespace SdmxMl.UC
{

    #region Interface IMultiLangControl
    public interface IMultiLangControl
    {
        MultiLang ML
        {
            get;
            set;
        }
        void RefreshText();
        void RefreshControl();
    }
    #endregion //Interface


    public partial class UcMlBase : UserControl, IMultiLangControl
    {
        private static string[] arrButHlp = { "Cut Internationalized text", 
                                                "Copy Internationalized text", 
                                                "Paste Internationalized text", 
                                                "Set all empty texts same (not language specific)" };

        protected RichTextBox[] arrTextBox;
        protected Label[] arrLabel;
        protected MultiLang ml;
        protected bool _isFinal;
        protected Button but;
        protected Button[] arrButBar;
        protected ToolTip toolTip; 

        public bool IsFinal
        {
            set
            {
                _isFinal = value;

                foreach (RichTextBox tb in arrTextBox) 
                    if (tb != null)
                        tb.ReadOnly = _isFinal;
                foreach (Button b in arrButBar)
                        b.Enabled = _isFinal == false;

                // Copy allowed
                arrButBar[1].Enabled = true;
            }
        }

        #region Proprties

        public MultiLang ML
        {
            get { return ml; }
            set 
            { 
                ml = value;

                // Paste state
                arrButBar[2].Enabled = _isFinal == false && SdmxMl.Manager.CopyPasteManager.Internationalized != null;
            }
        }

        #endregion

        public UcMlBase()
        {
            InitializeComponent();
            but = new Button();
            arrTextBox = new RichTextBox[LangSupport.Count];
            arrLabel = new Label[LangSupport.Count];
            arrButBar = new Button[4];
            this.components = new System.ComponentModel.Container();

        }

        /// <summary>
        /// Creates label and text control for each supported language
        /// </summary>
        /// <param name="panelBack">PContainer panel</param>
        /// <param name="multiHeight">if != 0 tells height of multiline textboxes</param>
        protected void InitializeTextontrols(Panel panelBack, int multiHeight)
        {
            int height = 26;
            int textBaseHeight = 20;
            bool multiLine = multiHeight > 0;

            int i = 0;
            foreach (string lg in LangSupport.SupportedLanguages)
            {
                arrLabel[i] = new Label();
                arrLabel[i].Location = new Point(4, 4 + i * (height + multiHeight));
                arrLabel[i].Text = lg;
                arrLabel[i].Width = 24;
                arrTextBox[i] = new RichTextBox();
                arrTextBox[i].Location = new Point(28, 4 + i * (height + multiHeight));
                arrTextBox[i].Width = 159;
                arrTextBox[i].Height = textBaseHeight + multiHeight;

                arrTextBox[i].Multiline = multiLine;
                if (multiHeight > 0)
                {
                    arrTextBox[i].ScrollBars = RichTextBoxScrollBars.Vertical;
                    arrTextBox[i].DetectUrls = true;
                    arrTextBox[i].LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.richTextBox_LinkClicked);

                }
                else
                    arrTextBox[i].ScrollBars = RichTextBoxScrollBars.None;

                arrTextBox[i].Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
                panelBack.Controls.Add(arrLabel[i]);
                panelBack.Controls.Add(arrTextBox[i]);

                ++i;
            }

            // Button for all Language same
            int yLoc = 4 + (LangSupport.Count) * (height + multiHeight);
            this.toolTip = new System.Windows.Forms.ToolTip(this.components);
            // 
            for (i = 0; i < 4; i++)
            {
                arrButBar[i] = new Button();
                this.toolTip.SetToolTip(arrButBar[i], arrButHlp[i]);
                arrButBar[i].BackColor = Color.Beige;
                arrButBar[i].Location = new Point(28 + i * arrButBar[0].Width, yLoc);
                arrButBar[i].Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
                panelBack.Controls.Add(arrButBar[i]);

            }
            arrButBar[0].Image = global::SdmxMl.Properties.Resources.cut;
            arrButBar[1].Image = global::SdmxMl.Properties.Resources.copy;
            arrButBar[2].Image = global::SdmxMl.Properties.Resources.paste;
            arrButBar[3].Image = global::SdmxMl.Properties.Resources.lst2icon;

            arrButBar[0].Click += new EventHandler(butCut_Click);
            arrButBar[1].Click += new EventHandler(butCopy_Click);
            arrButBar[2].Click += new EventHandler(butPaste_Click);
            arrButBar[3].Click += new EventHandler(butAllSame_Click);

        }

        private void richTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.LinkText);
        }



        void butAllSame_Click(object sender, EventArgs e)
        {
            // Find text to replicate
            string targetText = string.Empty;
            foreach (RichTextBox t in arrTextBox)
                if (t.Text != string.Empty && t.Text != MultiLang.UndefinedText)
                {
                    targetText = t.Text;
                    break;
                }

            // Replicate
            if (targetText.Length > 0)
            {
                //Checks to see Control is pressed and
                //force replacement of all, according of 1st
                if (Control.ModifierKeys == Keys.Control)
                {
                    foreach (RichTextBox t in arrTextBox)
                        t.Text = targetText;
                }
                else
                {
                    // Only for empty texts
                    foreach (RichTextBox t in arrTextBox)
                        if (t.Text == string.Empty || t.Text == MultiLang.UndefinedText)
                            t.Text = targetText;
                }
            }
                
        }

        void butCut_Click(object sender, EventArgs e)
        {
            MultiLang m = new MultiLang();
            SdmxMl.Manager.CopyPasteManager.Internationalized = m;

            int i = 0;
            foreach (string lg in LangSupport.SupportedLanguages)
            {
                m.UpdateForLanguage (lg, arrTextBox[i].Text);
                arrTextBox[i].Text = string.Empty;
                ++i;
            }
        }

        void butCopy_Click(object sender, EventArgs e)
        {
            MultiLang m = new MultiLang();
            SdmxMl.Manager.CopyPasteManager.Internationalized = m;

            int i = 0;
            foreach (string lg in LangSupport.SupportedLanguages)
            {
                m.UpdateForLanguage(lg, arrTextBox[i].Text);
                ++i;
            }
        }

        void butPaste_Click(object sender, EventArgs e)
        {
            // Find text to replicate
            if (SdmxMl.Manager.CopyPasteManager.Internationalized != null)
            {
                int i = 0;
                foreach (string lg in LangSupport.SupportedLanguages)
                {
                    arrTextBox[i].Text = SdmxMl.Manager.CopyPasteManager.Internationalized.GetForLanguage(lg);
                    ++i;
                }
            }
        }

        #region IMultiLangControl Members


        public void RefreshText()
        {
            if (ml != null)
            {
                int i = 0;
                foreach (string lg in LangSupport.SupportedLanguages)
                    ml.UpdateForLanguage(lg, arrTextBox[i++].Text);
            }
        }

        public void RefreshControl()
        {
            if (ml != null)
            {
                int i = 0;
                foreach (string lg in LangSupport.SupportedLanguages)
                    arrTextBox[i++].Text = ml.GetForLanguage(lg);
            }
        }
        #endregion



    }
}
